from PyQt4.QtGui import *
from PyQt4.QtCore import *


##
# Base class for application dialogs
# Contains a form layout and a button box Valid/Cancel
#
class Dialog(QDialog):
	def __init__(self, title, parent=None):
		QDialog.__init__(self, parent)
		self.setWindowTitle(title)

		self.validated = False
		layout = QVBoxLayout()
		self.setLayout(layout)

		# form layout
		form = QFormLayout()
		form.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)
		self.buildForm(form)
		layout.addLayout(form)

		# buttons
		but_cancel = QPushButton("Cancel")
		self.connect(but_cancel, SIGNAL("clicked()"), SLOT("close()"))

		but_valid = QPushButton("OK")
		self.connect(but_valid, SIGNAL("clicked()"), self.onValidated)

		buttons_box = QHBoxLayout()
		buttons_box.addWidget(but_cancel)
		buttons_box.addWidget(but_valid)
		layout.addLayout(buttons_box)

		self.layout().setSizeConstraint(QLayout.SetFixedSize)


	def buildForm(self, form):
		pass

	##
	# Valid-button callback
	#
	def onValidated(self):
		self.validated = True
		self.close()

	def isValidated(self):
		return self.validated
